from game.profile.models import profile
from game.combat.views import combatURLPath, showCombat
from django.shortcuts import redirect
from game.common.logger import logger

import re

class combatCheckMiddleware():
#Checks if a use is in combat, if so it will redirect the user
    def process_request(self, request):
        logger.debug("Middleware called, checking if player is in combat")
        match = re.compile("/"+combatURLPath)
        if request.user.is_authenticated() and not match.search(request.path):
            prof = profile.objects.get(user = request.user)
            if prof.isEngaged():
                logger.debug("User is in combat and tried to do something else. Path: %s didn't match combat path: %s"%(request.path, combatURLPath))
                return redirect(showCombat)
            else:
                logger.debug("Normal request")
                return None
            
        else:
            logger.debug("User isn't logged in or is already requesting combat")
            return None
    
    